import router from '@/router/main';
import { isNullPm } from '@/utils/baseMethods';
import { reactive, watch } from 'vue';

export default function importComfig() {
  /**
   * @param routerList 动态路由列表
   */
  const base = reactive<any>({
    routerList: null,
  });
  const init = () => {
    routerRead();
    watchRouter();
  };

  /**
   * @exports routerRead 读取路由路径
   */
  const routerRead = () => {
    base.routerList = [];
    const steam = router.currentRoute.value.matched;
    steam.forEach((value) => {
      base.routerList.push({
        path: value.path,
        name: value.name,
        title: value.meta.title,
      });
    });
  };

  /**
   * @exports routerGo 路由跳转
   * @param param 跳转的Name
   */
  const routerGo = (param) => {
    router.push(param);
  };

  /**
   * @exports watchRouter 路由动态监听
   */
  const watchRouter = () => {
    watch(
      () => router.currentRoute.value.matched,
      () => {
        if (!isNullPm(base.routerList)) routerRead();
      },
    );
  };

  return {
    base,
    init,
    routerGo,
    watchRouter,
  };
}
